package com.zengqingfa.shardingjdbc.demo.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zengqingfa.shardingjdbc.demo.entity.Bill;
import com.zengqingfa.shardingjdbc.demo.entity.BillRespDTO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @fileName: BillMapper
 * @author: zengqf3
 * @date: 2021-1-28 10:47
 * @description:
 */
public interface BillMapper extends BaseMapper<Bill> {

    /**
     * 根据主键查询
     *
     * @param id
     * @return
     */
    @Select("select * from bill where id = #{id} and tenant_code = #{tenantCode}")
    Bill selectByPrimary(@Param("id") Long id, @Param("tenantCode") String tenantCode);

    /**
     * 根据主键查询
     *
     * @param id
     * @return
     */
    @Select("select * from bill where id = #{id} ")
    Bill selectById(@Param("id") Long id);

    /**
     * 连表查询
     *
     * @param id
     * @return
     */
    @Select("select * from bill b inner join detail_bill d on b.id = d.bill_id "
            + "where b.tenant_code = #{tenantCode} and b.id=#{id}")
    List<BillRespDTO> queryBillAndDetail(@Param("id") Long id, @Param("tenantCode") String tenantCode);

    /**
     * 范围查询
     * @param lower
     * @param upper
     * @return
     */
    @Select("select * from bill where  tenant_code between  #{lower} and  #{upper}")
    List<Bill> between(@Param("lower") String lower, @Param("upper") String upper);

    /**
     * 分页查询1
     * @param pageNo
     * @param pageSize
     * @param dbIndex
     * @return
     */
    @Select("select * from ds0${dbIndex}.bill LIMIT ${(pageNo-1)*pageSize},${pageSize} ")
    List<Bill> selectPage(@Param("pageNo") int pageNo, @Param("pageSize") int pageSize,
            @Param("dbIndex") String dbIndex);

    /**
     * 分页查询2
     * @param pageNo
     * @param pageSize
     * @param dbIndex
     * @return
     */
    List<Bill> selectPage2(@Param("pageNo") int pageNo, @Param("pageSize") int pageSize,
            @Param("dbIndex") String dbIndex, @Param("tableIndex") String tableIndex);

    /**
     * 分页查询
     * @param pageNo
     * @param pageSize
     * @return
     */
    List<Bill> selectPage4(@Param("pageNo") int pageNo, @Param("pageSize") int pageSize);

    /**
     * 分页查询3
     * @param pageNo
     * @param pageSize
     * @return
     */
    List<Bill> selectPage3(@Param("pageNo") int pageNo, @Param("pageSize") int pageSize);
}
